package com.pengpeng.rocketmq.web;

import com.pengpeng.rocketmq.consumer.ScheduleConsumer;
import com.pengpeng.rocketmq.producer.OrderedProducer;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 彭鹏 顺序消息
 * @date 2021/10/25.
 */
@Slf4j
@RestController
@RequestMapping("/schedule")
public class ScheduleController {

    @Autowired
    private OrderedProducer producer;

    @RequestMapping("/scheduleProducer")
    public Object scheduleProducer() throws Exception {
        int totalMessagesToSend = 20;
        for (int i = 0; i < totalMessagesToSend; i++) {
            Message message = new Message(ScheduleConsumer.TOPIC,"SCHEDULE1", ("Hello scheduled message " + i).getBytes());

            /*
            *  delivered
            *  1s  5s  10s 30s 1m  2m  3m  4m  5m  6m  7m  8m  9m  10m 20m 30m 1h  2h
            *  1   2   3   4   5   6   7   8   9   10  11  12  13  14  15  16  17  18
            * */
            // This message will be delivered to consumer 10 seconds later.
            message.setDelayTimeLevel(3);
            // Send the message
            SendResult sendResult = producer.getProducer().send(message);
            System.out.printf("%s%n", sendResult);
        }
        return "成功";
    }
}

